package com.huffman.smart_picture.auth;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;
import com.huffman.smart_picture.auth.model.SpaceUserAuthConfig;
import com.huffman.smart_picture.auth.model.SpaceUserRole;
import com.huffman.smart_picture.common.exception.ErrorEnum;
import com.huffman.smart_picture.domain.enums.SpaceUserEnum;
import com.huffman.smart_picture.utils.ThrowUtil;
import org.springframework.stereotype.Component;

/**
 * @author yrforest
 * @version 1.0
 * @description: 空间用户权限管理
 * @date 2025/6/28 20:44
 */
@Component
public class SpaceUserAuthManager {

    private static final SpaceUserAuthConfig SPACE_USER_AUTH_CONFIG;

    static {
        String json = ResourceUtil.readUtf8Str("auth/auth.json");
        SPACE_USER_AUTH_CONFIG = JSONUtil.toBean(json, SpaceUserAuthConfig.class);
    }

    public SpaceUserRole getSpaceUserRole(Integer value) {
        SpaceUserEnum spaceUserEnum = SpaceUserEnum.getSpaceUserEnum(value);
        ThrowUtil.throwIf(spaceUserEnum == null, ErrorEnum.SYSTEM_ERROR, "用户权限获取失败");
        return SPACE_USER_AUTH_CONFIG.getRoles()
                .stream()
                .filter(role -> role.getRole().equals(spaceUserEnum.getSpaceRole()))
                .findFirst()
                .orElse(null);
    }
}
